import * as query from '../services/userIntegral';

const namespace = 'userIntegral';

export default {
  namespace,

  state: {
    data: {
      list: [],
      pagination: {},
    }
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(query.fetch, payload);
      const { errno, pagination = {}, data = [] } = response;

      if (!errno) {
        const results = {
          pagination: {
            total: pagination.count,
            pageSize: pagination.size,
            current: pagination.from,
          },
          list: data,
        }

        yield put({
          type: 'save',
          payload: results,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};